package code.tools.commponents.button
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.font.FontManager;
	import code.structure.manager.sound.MySoundManager;
	import code.tools.commponents.Component;
	import code.tools.commponents.Helpers;
	import code.tools.sound.src.SoundName;
	import code.untils.FiltersEnum;
	import code.untils.UiNameConst;
	
	import com.patience.utils.FilterUtil;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import flashx.textLayout.elements.BreakElement;
	
	/**
	 *购买按钮 
	 * 默认宽高：115*30
	 * @author liangqian
	 * 
	 */	
	public class BuyBtn extends Component
	{
		/**
		 *购买按钮类型：cash 
		 */		
		public static const BUY_STYLE_CASH:int = 10;
		/**
		 *购买按钮类型：coin
		 */		
		public static const BUY_STYLE_COIN:int = 20;
		/**
		 *购买按钮类型：love 
		 */		
		public static const BUY_STYLE_LOVE:int = 30;
		/**
		 *购买按钮类型：装饰值 
		 */		
		public static const BUY_STYLE_DECORATE:int = 40;
		/**
		 * 购买按钮类型：用于采购车界面的货币
		 */		
		public static const BUY_STYLE_ESMQ_COIN:int = 50;
		/**
		 *购买按钮类型：用于排行榜的积分兑换的 
		 */		
		public static const BUY_STYLE_RANK_SCORE:int = 60;
		/**
		 * 购买按钮类型：用于商店go到收集兑换的
		 */		
		public static const BUY_STYLE_MARKET_EXCHANGE:int = 70;
		/**
		 * 购买按钮类型：用于店铺订单弹窗上的下订单按钮
		 */		
		public static const BUY_STYLE_ORDER:int = 80;
		/**
		 *购买按钮类型：用于店铺订单弹窗上的升级按钮 
		 */		
		public static const BUY_STYLE_UPGRADE:int = 90;
		/**
		 * 购买按钮类型：带个锁图标的
		 */		
		public static const BUY_STYLE_UNLOCK:int = 100;
		/**
		 * 购买按钮类型：带个换装图标的
		 */		
		public static const BUY_STYLE_DRESS:int = 110;
		/****************************按钮滤镜样式枚举*******************************/
		/**
		 *白色滤镜 
		 */		
		public static const BTN_FILTERS_WHITE:int = 10;
		/**
		 *黑色滤镜 
		 */		
		public static const BTN_FILTERS_BLACK:int = 20;
		/**
		 *无滤镜 
		 */		
		public static const BTN_FILTERS_NONE:int = 30;
		/**
		 *黑色滤镜 2
		 */		
		public static const BTN_FILTERS_BLACK2:int = 40;
		
		private static const DEFAULT_WIDTH:int = 115;
		
		private var _clickHandler:Function;
		private var _buyStyle:int;
		
		private var bgMc:MovieClip;
		private var costIcon:Sprite;
		
		private var costTf:TextField;
		private var textcolor:uint;
		
		private var _bgWidth:Number;
		private var _bgHeight:Number;
		private var _costIconScaleXy:Number;
		private var _tfSize:int;
		private var _costIconXpos:Number;
		private var _filtersStyle:Number;
		private var _costIconIsCenter:Boolean;
		private var _fontName:String;
		private var _textAuto:String;
		/**
		 * 
		 * @param parent 父级
		 * @param xpos
		 * @param ypos
		 * @param buyStyle 购买样式，见本类枚举
		 * @param clickHandler 点击事件
		 * @param filtersStyle 滤镜样式，见本类枚举，默认无滤镜
		 * @param wid 设置按钮背景的宽度，默认-1，表示用默认的
		 * @param hei 设置按钮背景的高度，默认-1，表示用默认的
		 * @param costIconScaleXy 花费图标的缩放
		 * @param tfSize 文字大小，默认15号字
		 * @param costIconXpos 花费图标的X轴位置，100表示，按默认的来
		 * @param fontName 见枚举 FontManager.getInstance()，默认 FontManager.getInstance().embeddedFonDName
		 * @param textAuto autosize
		 * 
		 */		
		public function BuyBtn(parent:DisplayObjectContainer, xpos:Number, ypos:Number,buyStyle:int,clickHandler:Function = null,
							   filtersStyle:int = 30,wid:Number = -1,hei:Number = -1,costIconScaleXy:Number = 1,
							   tfSize:int = 18,costIconXpos:Number = 100,costIconIsCenter:Boolean = false,fontName:String = 'D',textAuto:String = 'center')
		{
			_buyStyle = buyStyle;
			_filtersStyle = filtersStyle;
			_bgWidth = wid == -1 ? DEFAULT_WIDTH : wid;
			_bgHeight = hei == -1 ? 30 : hei;
			_costIconScaleXy = costIconScaleXy;
			_tfSize = tfSize;
			_clickHandler = clickHandler;
			_costIconXpos = costIconXpos;
			_costIconIsCenter = costIconIsCenter;
			_fontName = fontName == 'D' ? FontManager.getInstance().embeddedFonDName : fontName;
			_textAuto = textAuto;
			super(parent, xpos, ypos);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			buttonMode = true;
			mouseChildren = false;
			
			var costTfXpos:Number = 45;
			var costTfWid:Number = 66;
			var bgMcYpos:Number = 6;
			switch(_buyStyle)
			{
				case BUY_STYLE_CASH:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'blueBtn_with_cash') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'withCashBtn_big_cash') as Class) as Sprite;
					textcolor = 0x3E6A83;
					costIcon.x = 5;
					//costIcon.y = 5;
					break;
				case BUY_STYLE_COIN:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'buyBtn_coin') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'coin_with_btn') as Class) as Sprite;
					textcolor = 0x4C7A20;
					costIcon.x = 9;
					//costIcon.y = 1;
					break;
				case BUY_STYLE_LOVE:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'buyBtn_coin') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'love_with_btn') as Class) as Sprite;
					textcolor = 0x4C7A20;
					costIcon.x = 6;
					//costIcon.y = 3;
					break;
				case BUY_STYLE_DECORATE:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'greenBtn_small1') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.ALERT_VIEW,'decarateIcon') as Class) as Sprite;
					textcolor = 0x4C7A20;
					costIcon.x = 1;
					break;
				case BUY_STYLE_ESMQ_COIN:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'buyBtn_coin') as Class) as MovieClip;
//					bgMc.width = 74;
//					bgMc.height = 24;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'esmqCoin') as Class) as Sprite;
					costIcon.x = 1;
					textcolor = 0x4C7A20;
					costTfWid = 38;
					costTfXpos = 36;
					break;
				case BUY_STYLE_RANK_SCORE:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'buyBtn_coin') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'scoreCoin') as Class) as Sprite;
					costIcon.x = 1;
					textcolor = 0x4C7A20;
					costTfWid = 38;
					costTfXpos = 36;
					break;
				case BUY_STYLE_MARKET_EXCHANGE:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'yellowBtn_small2') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'exchangeIcon') as Class) as Sprite;
					costIcon.x = 6;
					textcolor = 0x927326;
					break;
				case BUY_STYLE_ORDER:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'yellowBtn_small1') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'orderIcon') as Class) as Sprite;
					textcolor = 0x927326;
					break;
				case BUY_STYLE_UPGRADE:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'greenBtn_small1') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'upgradeIcon') as Class) as Sprite;
					textcolor = 0x4C7A20;
					break;
				case BUY_STYLE_UNLOCK:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'greenBtn_small1') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'lock2') as Class) as Sprite;
					textcolor = 0x4C7A20;
					break;
				case BUY_STYLE_DRESS:
					bgMc = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'greenBtn_small1') as Class) as MovieClip;
					costIcon = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'dressIcon') as Class) as Sprite;
					textcolor = 0x4C7A20;
					break;
			}
			
			if(_bgWidth != -1)
				refreshBtn(1);
			if(_bgHeight != -1)
			{
				bgMc.height = _bgHeight;
			}
			costIcon.scaleX = costIcon.scaleY = _costIconScaleXy;
			setSize(bgMc.width,bgMc.height);
			if(_costIconXpos != 100)
				costIcon.x = _costIconXpos;
			costIcon.y = _height - costIcon.height;
			if(_buyStyle == BUY_STYLE_CASH)
			{
				if(!_costIconIsCenter)
					costIcon.y += 6;
				else
					costIcon.y =  (_height - costIcon.height)*.5 + 2;
			}
			costTfXpos =  costIcon.x + costIcon.width - 4;
			costTfWid = width - costTfXpos;
			addChild(bgMc);
			addChild(costIcon);
			switch(_filtersStyle)
			{
				case BTN_FILTERS_WHITE:
					bgMc.filters = [FiltersEnum.BUY_BTN_DROPSHADOWFILTER];
					break;
				case BTN_FILTERS_BLACK:
					bgMc.filters = [FiltersEnum.BTN_NEW_BIE_DROPSHADOWFILTER];
					break;
				case BTN_FILTERS_BLACK2:
					bgMc.filters = [FiltersEnum.CHARTS_PRIZE_DROPSHADOWFILTER];
					break;
				default:
					bgMc.filters = null;
					break;
			}
			
			costTf = Helpers.FormatTextField(this,costTfXpos,bgMc.y - 2,costTfWid,_textAuto,textcolor,_fontName,_tfSize);
			costTf.filters = [FiltersEnum.SHOP_ITEM_BUY_PRICE_DROPSHADOWFILTER];
			costTf.mouseEnabled = false;
			addEventListener(MouseEvent.MOUSE_OVER , overHandler);
			addEventListener(MouseEvent.MOUSE_DOWN , downHandler);
		}
		
		override protected function init():void
		{
			setSize(117,32);
			super.init();
		}
		
		public function set cost(c:int):void
		{
			costTf.text = c+'';
			costTf.y = (_height - costTf.textHeight)*.5 - 5;
		}
		
		public function get cost():int
		{
			return int(costTf.text);
		}
		
		public function setText(str:String):void
		{
			costTf.text = str;
			costTf.y = (_height - costTf.textHeight)*.5 - 3;
			refresh();
		}
		
		private function refresh():void
		{
			if(_bgWidth == DEFAULT_WIDTH)
			{
				_bgWidth = costTf.x + costTf.textWidth + 12;
				costTf.x = costIcon.x + costIcon.width + ((_bgWidth - (costIcon.x + costIcon.width)) - costTf.textWidth)*.5;
				_bgWidth += 5;
				(bgMc.getChildAt(0)as MovieClip).width = _bgWidth;
				setSize(bgMc.width,bgMc.height);
			}
		}
		
		private var _enable:Boolean = true;
		public function set enable(v:Boolean):void
		{
			_enable = v;
			if(v)
			{
				buttonMode = true;
				mouseEnabled = true;
				costTf.textColor = textcolor;
				costIcon.filters = null;
				refreshBtn(1);
			}
			else
			{
				buttonMode = false;
				mouseEnabled = false;
				costIcon.filters = [FilterUtil.grayColorFilter];
				costTf.textColor = 0x666666;
				refreshBtn(3);
			}
			bgMc.width =  _width;
		}
		
		public function get enable():Boolean
		{
			return _enable;
		}
		
		private function overHandler(e:MouseEvent):void
		{
			addEventListener(MouseEvent.MOUSE_OUT , outHandler);
			if(!enable)return;
			MySoundManager.playSound(SoundName.MOUSE_OVER, 0.2);
			refreshBtn(2);
		}
		
		private function outHandler(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_OUT , outHandler);
			if(!enable)return;
			refreshBtn(1);
		}
		
		private function downHandler(e:MouseEvent):void
		{
			if(_clickHandler!=null)
			{
				_clickHandler(this);
			}
		}
		
		private function refreshBtn(frame:int):void
		{
			bgMc.gotoAndStop(frame);
			if(_bgWidth != -1)
				(bgMc.getChildAt(0)as MovieClip).width = _bgWidth;
		}
	}
}